package com.chat.datea.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;

import com.chat.datea.utils.cache.MyConfig;
import com.chat.datea.utils.download.DownloadProgressListener;
import com.chat.datea.utils.download.FileDownloader;

import java.io.File;

/**
 * Created by dllo on 15/11/2.
 * 王锦予
 */
public class DownLoderService extends Service {
    private int max, progress;
    public DownloadTask task;
    private Handler handler = new UIHandler();
    private static final int PROCESSING = 1;
    private static final int FAILURE = -1;
    private Context context;



    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new DownLoder();
    }


    class DownLoder extends Binder implements DownLoderInterface {

        @Override
        public void downLoder(String path, File savDir) {
            download(path, savDir);
        }

        @Override
        public int getMax() {
            return max ;
        }

        @Override
        public int getProgress() {
            return progress;
        }


    }

    /**
     * 下载方法
     */
    public void download(String path, File savDir) {
        path = MyConfig.getSharePreStr(DownLoderService.this, "link", "url");
        task = new DownloadTask(path, savDir);
        new Thread(task).start();

        Log.i("--------base-----------", "path = " + path);
        Log.i("--------dir-----------", "dir = " + savDir);
    }

    //下载的线程&&下载进度条
    private final class UIHandler extends Handler {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case PROCESSING: // 更新进度
//                    MyConfig.getSharePreInt(context, "fileSize", "fileSize");

                    break;
                case FAILURE: // 下载失败
                    break;
            }
        }

    }


    /**
     * UI控件画面的重绘(更新)是由主线程负责处理的，如果在子线程中更新UI控件的值，更新后的值不会重绘到屏幕上
     * 一定要在主线程里更新UI控件的值，这样才能在屏幕上显示出来，不能在子线程中更新UI控件的值
     * <p/>
     * 下载任务的线程 内部类
     */
    public final class DownloadTask implements Runnable {

        /**
         * path就是下载链接
         * <p/>
         * path拼接成下载地址即可
         * <p/>
         * sdcard/dowmload
         */
        private String path;
        private File saveDir;
        private FileDownloader loader;

//
//             Intent intent = getIntent();
//             String path = intent.getStringExtra("show_link");


        //构造方法
        public DownloadTask(String path, File saveDir) {
            this.path = path;
            this.saveDir = saveDir;
        }

        /**
         * 退出下载
         */
        public void exit() {
            if (loader != null)
                loader.exit();
        }


        //下载进度条
        DownloadProgressListener downloadProgressListener = new DownloadProgressListener() {
            @Override
            public void onDownloadSize(int size) {
                Message msg = new Message();
                msg.what = PROCESSING;
                msg.getData().putInt("size", size);
                handler.sendMessage(msg);
            }
        };


        //线程执行方法
        //进度条
        public void run() {
            // 实例化一个文件下载器
            loader = new FileDownloader(getApplicationContext(), path,
                    saveDir, 3);
            Log.i("--------run-----------", "path = " + path);
            Log.i("--------run-----------", "dir = " + saveDir);
            max = loader.getFileSize();
//                // 设置进度条最大值
//                progressBar.setMax(loader.getFileSize());
            try {
                loader.download(downloadProgressListener);
            } catch (Exception e) {
                e.printStackTrace();
                handler.sendMessage(handler.obtainMessage(FAILURE)); // 发送一条空消息对象
            }
        }
    }

    public int getMax() {
        return max;
    }

    public int getProgress() {
        return progress;
    }

}
